<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form inline :model="form" ref="form" class="demo-form-inline">
          <el-form-item prop="name" label="资源名称">
            <el-input v-model="form.name" placeholder="资源名称"></el-input>
          </el-form-item>
          <el-form-item prop="url" label="资源路径">
            <el-input v-model="form.url" placeholder="资源路径"></el-input>
          </el-form-item>
          <el-form-item prop="categoryId" label="资源分类">
            <el-select
              v-model="form.categoryId"
              placeholder="资源分类"
              clearable
            >
              <el-option
                v-for="item in categroies"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              :disabled="isLoading"
              type="primary"
              @click="onSubmit">查询</el-button>
            <el-button
              :disabled="isLoading"
              @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="resources" style="width: 100%" v-loading="isLoading">
        <el-table-column type="index" label="编号" width="50">
        </el-table-column>
        <el-table-column prop="name" label="资源名称"> </el-table-column>
        <el-table-column prop="url" label="资源路径"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column prop="createdTime" label="添加时间"> </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button
              @click="handleEdit(scope.$index, scope.row)"
              type="text"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              @click="handleDelete(scope.$index, scope.row)"
              type="text"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :disabled="isLoading"
        :current-page.sync="form.current"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="form.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { Form } from 'element-ui'
import { getResourcePages } from '@/services/resource'
import { getResourceCategories } from '@/services/resource-category'
export default Vue.extend({
  name: 'ResourceList',
  data () {
    return {
      resources: [],
      total: 0,
      form: {
        name: '',
        url: '',
        categoryId: null,
        size: 5,
        current: 1
      },
      categroies: [],
      isLoading: true
    }
  },

  created () {
    this.loadResourceCategories()
    this.loadResources()
  },

  methods: {
    onReset () {
      (this.$refs.form as Form).resetFields()
      this.form.current = 1
      this.loadResources()
    },
    onSubmit () {
      console.log('submit')
      this.form.current = 1
      this.loadResources()
    },

    async loadResourceCategories () {
      const { data } = await getResourceCategories()
      console.log(data)
      this.categroies = data.data
    },

    async loadResources () {
      this.isLoading = true
      const { data } = await getResourcePages(this.form)
      console.log(data)
      if (data.code === '000000') {
        const { records, total } = data.data
        this.resources = records
        this.total = total
        this.isLoading = false
      }
    },
    handleEdit () {
      console.log('h')
    },
    handleDelete () {
      console.log('h')
    },
    handleSizeChange (val: number) {
      this.form.current = 1
      this.form.size = val
      this.loadResources()
    },
    handleCurrentChange (val: number) {
      this.form.current = val
      this.loadResources()
    }
  }
})
</script>

<style lang="scss" scoped>
.el-pagination {
  margin: 10px 0;
  text-align: right;
}
</style>
